- Print
- DarkLight
Article summary
Did you find this summary helpful?
Thank you for your feedback!
test openai accuracy of chatbot
import openai
# Set your OpenAI API key
api_key = "your_openai_api_key"
openai.api_key = api_key
def test_chatbot(prompt):
try:
# Request a completion from the model
response = openai.Completion.create(
engine="davinci", # You can choose other engines like "curie", "babbage", etc.
prompt=prompt,
max_tokens=50, # Adjust the length of the response as needed
temperature=0.7, # Controls the randomness of the response, 0.7 is a common value
n=1, # Number of completions to generate
stop=None, # Stop generating when this token is reached
timeout=None # Time limit for generating a response
)
return response.choices[0].text.strip()
except Exception as e:
print("Error:", e)
return None
# Test cases
test_cases = [
"How does photosynthesis work?",
"What is the capital of France?",
"Tell me about the history of artificial intelligence.",
"What are the symptoms of COVID-19?",
"¿Cómo se dice 'hello' en español?",
"Qu'est-ce que la philosophie?",
"Was sind die Hauptbestandteile eines Computers?",
"Какие языки программирования самые популярные?",
]
# Test the chatbot for each test case
for case in test_cases:
print("Prompt:", case)
response = test_chatbot(case)
if response:
print("Response:", response)
else:
print("Failed to generate a response.")
print()
Next lets use openai api to interact with the model using certain libabry like okhttp.
import okhttp3.*;
import java.io.IOException;
public class ChatbotTester {
private static final String OPENAI_API_KEY = "your_openai_api_key";
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// Test cases
String[] testCases = {
"How does photosynthesis work?",
"What is the capital of France?",
"Tell me about the history of artificial intelligence.",
"What are the symptoms of COVID-19?",
"¿Cómo se dice 'hello' en español?",
"Qu'est-ce que la philosophie?",
"Was sind die Hauptbestandteile eines Computers?",
"Какие языки программирования самые популярные?"
};
for (String testCase : testCases) {
try {
String response = getResponse(client, testCase);
System.out.println("Prompt: " + testCase);
System.out.println("Response: " + response);
System.out.println();
} catch (IOException e) {
System.err.println("Error occurred: " + e.getMessage());
}
}
}
private static String getResponse(OkHttpClient client, String prompt) throws IOException {
RequestBody requestBody = new FormBody.Builder()
.add("prompt", prompt)
.add("max_tokens", "50") // Adjust the length of the response as needed
.add("temperature", "0.7") // Controls the randomness of the response, 0.7 is a common value
.add("n", "1") // Number of completions to generate
.build();
Request request = new Request.Builder()
.url("https://api.openai.com/v1/completions")
.header("Authorization", "Bearer " + OPENAI_API_KEY)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
String responseBody = response.body().string();
// Extracting response from JSON
return responseBody.split("\"")[7];
}
}
}
great huray!!!, lets go to new languge where we can perfrom api integration using http client.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
private static readonly string OpenAI_API_KEY = "your_openai_api_key";
static async Task Main(string[] args)
{
string[] testCases = {
"How does photosynthesis work?",
"What is the capital of France?",
"Tell me about the history of artificial intelligence.",
"What are the symptoms of COVID-19?",
"¿Cómo se dice 'hello' en español?",
"Qu'est-ce que la philosophie?",
"Was sind die Hauptbestandteile eines Computers?",
"Какие языки программирования самые популярные?"
};
foreach (string testCase in testCases)
{
string response = await GetResponse(testCase);
Console.WriteLine("Prompt: " + testCase);
Console.WriteLine("Response: " + response);
Console.WriteLine();
}
}
static async Task<string> GetResponse(string prompt)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {OpenAI_API_KEY}");
var requestBody = new StringContent(
$"{{\"prompt\": \"{prompt}\", \"max_tokens\": 50, \"temperature\": 0.7, \"n\": 1}}",
Encoding.UTF8,
"application/json");
HttpResponseMessage response = await client.PostAsync("https://api.openai.com/v1/completions", requestBody);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
// Extracting response from JSON
return responseBody.Split("\"")[7];
}
else
{
throw new Exception($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
}
}
OpenAI api key :
{
"api_key": "your_openai_api_key",
"test_cases": [
"How does photosynthesis work?",
"What is the capital of France?",
"Tell me about the history of artificial intelligence.",
"What are the symptoms of COVID-19?",
"¿Cómo se dice 'hello' en español?",
"Qu'est-ce que la philosophie?",
"Was sind die Hauptbestandteile eines Computers?",
"Какие языки программирования самые популярные?"
],
"chatbot_settings": {
"engine": "davinci",
"max_tokens": 50,
"temperature": 0.7,
"n": 1,
"stop": null,
"timeout": null
}
}
Was this article helpful?